﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Collections;

namespace Quanly_dethi.Class
{
    class csC_Lop
    {
        private ketnoi kn;

        internal ketnoi Kn
        {
            get { return kn; }
            set { kn = value; }
        }
        private string databasename;

        public string Databasename
        {
            get { return databasename; }
            set { databasename = value; }
        }
        public csC_Lop()
        {
            Kn = new ketnoi();
            databasename = "";
        }
        public csC_Lop(string servername, string user, string pass, string dbname )
        {
            Kn = new ketnoi(servername, user, pass);
            databasename = dbname;
        }
        public void Insert(csE_lop x)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "insert into Lop(Tenlop) values (N'" + x.Tenlop.ToString() + "')";
            db.ExecuteWithResults(s);
        }
        public DataTable Getlop(csE_lop u)
        {
            Database db = Kn._Server.Databases[databasename];
            string s = "select top 1 Malop, Tenlop from Lop order by Malop DESC";
            DataTable k = db.ExecuteWithResults(s).Tables[0];
            return k;
        }
        public Boolean update(string  lopmoi )
        {
            Database db =Kn._Server.Databases[databasename];
            string s ="RESTORE DATABASE [TTN] FROM DISK =" + "'" + lopmoi + "'" + " WITH FILE = 1, NOUNLOAD, STATS = 10";
            DataTable k =db.ExecuteWithResults(s).Tables[0];
            if (k.Rows.Count > 0 )
            {
                return true;
            }
            return false;
        }
        public DataTable selectall()
        {
            Database db = Kn._Server.Databases[databasename];
            return db.ExecuteWithResults("select * from Lop").Tables[0];

        }
        public ArrayList SellectArr()
        {
            Database db = Kn._Server.Databases[databasename];
            DataTable dt = db.ExecuteWithResults("select MaLop,TenLop from Lop").Tables[0];
            ArrayList ar = new ArrayList();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                item t = new item(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString());
                ar.Add(t);

            }
            return ar;
        }
        public void Update(csE_lop x)
        {
            string t = "update Lop set Tenlop= '" + x.Tenlop + "' where Malop='" + x.Malop + "'";
            Database db = Kn._Server.Databases[databasename];
            db.ExecuteWithResults(t);
        }
        //public void Update(csE_Admin x)
        //{
         //   string t = "update NguoiDung set TenND=N'" + x.Tennguoidung + "', MaQuyen='" + x.Maquyen + "', Matkhau='" + x.Matkhau + "', Xoa='0' where MaND='" + x.Manguoidung + "'";
        //    Database db = Kn._Server.Databases[databasename];
        //    db.ExecuteWithResults(t);

        //} string s = "update SV set Tensv='" + x.Tensv + "', Magv='" + x.Magv + "' where Masv='" + x.Masv + "'";
            //kn.Set(s);
    }
}
